package com.EventHandler;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.Dao.CommentDao;
import com.Dao.LikesDao;
import com.Dao.UserDao;
import com.example.myapplication.ArticleActivity;
import com.example.myapplication.R;
import com.news.model.Comment;
import com.news.model.User;

/**
 * 负责处理用户交互事件。
 */
public class EventHandler {
    private Context context;
    private LikesDao likesDao;
    private CommentDao commentDao;
    private UserDao userDao;
    private int userId;
    private int newsId;
    private String username;
    private CommentManager commentManager;
    private Button btn_follow;
    private Button nav_btn_send;
    private ImageButton nav_btn_like;
    private ImageButton nav_btn_favorite;
    private ImageButton nav_btn_share;


    //构造函数
    public EventHandler(Context context, int userId, int newsId,String username, CommentManager commentManager) {
        this.context = context;
        this.userId = userId;
        this.newsId = newsId;
        this.username = username;
        this.commentManager = commentManager;
        likesDao = new LikesDao(context);
        commentDao = new CommentDao(context);
        userDao = new UserDao(context);
    }

    //初始化ui控件 所有的按钮
    public void initializeUI() {
        btn_follow = ((ArticleActivity) context).findViewById(R.id.btn_follow);
        nav_btn_send = ((ArticleActivity) context).findViewById(R.id.nav_btn_send);
        nav_btn_like = ((ArticleActivity) context).findViewById(R.id.nav_btn_like);
        nav_btn_favorite = ((ArticleActivity) context).findViewById(R.id.nav_btn_favorite);
        nav_btn_share = ((ArticleActivity) context).findViewById(R.id.nav_btn_share);

        setListeners();
    }


    public void loadStatus() {
        // 如果需要加载特定的数据，比如点赞状态或收藏状态，可以在这里实现
        // 例如，加载用户是否已经点赞或收藏了这篇新闻
        boolean isLiked = likesDao.isNewsLiked(userId, newsId);
        boolean isFavorited = likesDao.isNewsFavorited(userId, newsId);

        nav_btn_like.setImageResource(isLiked ? R.drawable.baseline_favorite_24 : R.drawable.baseline_favorite_border_24);
        nav_btn_favorite.setSelected(isFavorited);
    }

    public void setListeners() {
        btn_follow = ((ArticleActivity) context).findViewById(R.id.btn_follow);
        nav_btn_send = ((ArticleActivity) context).findViewById(R.id.nav_btn_send);
        nav_btn_like = ((ArticleActivity) context).findViewById(R.id.nav_btn_like);
        nav_btn_favorite = ((ArticleActivity) context).findViewById(R.id.nav_btn_favorite);
        nav_btn_share = ((ArticleActivity) context).findViewById(R.id.nav_btn_share);

        btn_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleFollow();
            }
        });

        nav_btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleSendComment(view);
            }
        });

        nav_btn_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleLike();
            }
        });

        nav_btn_favorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleFavorite();
            }
        });

        nav_btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleShare();
            }
        });
    }


    private void handleFollow() {
        TextView author = ((ArticleActivity) context).findViewById(R.id.author);
        User authorUser = userDao.getUserByName(author.getText().toString());
        if (authorUser != null) {
            boolean success = likesDao.followAuthor(userId, authorUser.getUserId());
            if (success) {
                Toast.makeText(context, "关注成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "关注失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "作者不存在", Toast.LENGTH_SHORT).show();
        }
    }

    private void handleSendComment(View view) {
        EditText nav_commit_edit = ((ArticleActivity) context).findViewById(R.id.nav_commit_edit);
        String comment = nav_commit_edit.getText().toString();
        if (!comment.isEmpty()) {
            long currentTime = System.currentTimeMillis();
            SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
            Comment newComment = new Comment(newsId, userId,comment, currentTime,username);
            commentDao.insertNews(newComment);
            //commentManager.loadComments(view);
            nav_commit_edit.setText("");
            ((ArticleActivity) context).addCommentAndRefresh(comment);
        }
    }

    private void handleLike() {
        boolean isLiked = likesDao.isNewsLiked(userId, newsId);
        long currentTimeMillis = System.currentTimeMillis();

        if (isLiked) {
            boolean success = likesDao.unlikeNews(userId, newsId, -1);
            if (success) {
                nav_btn_like.setImageResource(R.drawable.baseline_favorite_border_24);
                Toast.makeText(context, "取消点赞成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "取消点赞失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            boolean success = likesDao.likeNews(userId, newsId, currentTimeMillis);
            if (success) {
                nav_btn_like.setImageResource(R.drawable.baseline_favorite_24);
                Toast.makeText(context, "点赞成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "点赞失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void handleFavorite() {
        boolean success = likesDao.favoriteNews(userId, newsId);
        if (success) {
            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "收藏失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void handleShare() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "分享新闻内容");
        context.startActivity(Intent.createChooser(shareIntent, "分享新闻"));
    }

    public void close() {
        likesDao.close();
        commentDao.close();
    }

}
